import {Component, OnInit} from '@angular/core';
import {CooperationService} from '../cooperation.service';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
  selector: 'app-cooperation-data',
  templateUrl: './cooperation-data.component.html',
  styleUrls: ['./cooperation-data.component.scss']
})
export class CooperationDataComponent implements OnInit {
  query = {
    date: [new Date(+new Date() - 1000 * 3600 * 24 * 30), new Date()],
    appId: [],
    // 当前页
    current: 1,
    // 分页大小
    size: 20,
  };
  listOfData = {
    current: 1,
    total: 100,
    size: 20,
    records: []
  };
  // 统计数据
  dataCount = [{
    registerCountSum: '', // 注册数合计
    heDivideAmountSum: '', // 充值金额合计
    payCostSum: '', // 支付通道成本合计
    myDivideAmountSum: '', // 我方分成金额合计
    myDivideProfitSum: '', // 厂商分成金额合计
    rechargeAmountSum: '', // 我方利润合计
  }];
  modal = {
    modalVisible: false,
    operationType: '',
    loading: false,
  };
  modalMultiple = {
    modalVisible: false,
    operationType: '',
    loading: false,
    productId: '',
    data: [
      {
        date: '',
        registerCount: 0,
        rechargeAmount: 0,
      }
    ]
  };
  formData = {
    id: '',
    date: '',
    productId: '',
    registerCount: 0,
    rechargeAmount: 0,
  };
  appOptions: any[] = [];

  constructor(
    private cooperationService: CooperationService,
    private nzMessageService: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.queryList();
    this.cooperationService.getApp(1, 9999, '').then((res: any) => {
      if (res.code === 0) {
        this.appOptions = res.data.records;
      }
    });
  }

  queryBtn() {
    this.query.current = 1;
    this.queryList();
  }

  resetQuery() {
    this.query = {
      date: [],
      appId: [],
      current: 1,
      size: 20,
    };
    this.queryList();
  }

  // 查询列表
  queryList(): void {
    this.cooperationService.getData(
      this.query.current,
      this.query.size,
      this.query.appId.join(','),
      this.cooperationService.formatTime(this.query.date[0], 'date'),
      this.cooperationService.formatTime(this.query.date[1], 'date')
    ).then((res: any) => {
      if (res.code === 0) {
        this.listOfData = res.data.list;
        this.dataCount = [...res.data];
      }
    });
  }

  onPageIndexChange(index: number) {
    this.query.current = index;
    this.queryList();
  }

  onPageSizeChange(size: number) {
    this.query.size = size;
    this.queryList();
  }

  // 显示添加分类模态框
  add() {
    this.modal.modalVisible = true;
    this.modal.operationType = '添加';
  }

  // 显示添加分类模态框
  addMultiple() {
    this.modalMultiple = {
      modalVisible: true,
      operationType: '批量添加',
      loading: false,
      productId: '',
      data: []
    };
    let i = 0;
    while (i < 8) {
      i++;
      this.modalMultiple.data.push({
        date: '',
        registerCount: 0,
        rechargeAmount: 0,
      });
    }
  }

  // 查看
  view(data: any) {
    this.modal.modalVisible = true;
    this.modal.operationType = '查看';
    this.formData = data;
  }


  // 编辑
  edit(data: any) {
    this.modal.modalVisible = true;
    this.modal.operationType = '编辑';
    this.formData = JSON.parse(JSON.stringify(data));
  }

  // 模态框取消按钮
  handleModalCancel() {
    this.resetFormData();
  }

  // 模态框确认按钮监听
  handleModalOk() {
    if (!this.formData.date) {
      this.nzMessageService.warning('请选择日期');
      return;
    }
    if (!this.formData.productId) {
      this.nzMessageService.warning('请选择应用');
      return;
    }
    this.formData.date = this.cooperationService.formatTime(this.formData.date, 'date');
    this.cooperationService.addOrModifyData(this.formData, this.modal.operationType === '添加').then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.queryList();
      }
    }).finally(() => {
      this.resetFormData();
    });
  }

  // 多个模态框确认按钮监听
  handleMultipleModalOk() {
    if (!this.modalMultiple.productId) {
      this.nzMessageService.warning('请选择应用');
      return;
    }
    this.modalMultiple.data.forEach(m => {
      if (m.date) {
        m.date = this.cooperationService.formatTime(m.date, 'date');
        return;
      }
    });
    this.modalMultiple.data = this.modalMultiple.data.filter(m => m.date);
    const recordList = this.modalMultiple.data.map(d => {
      return {
        productId: this.modalMultiple.productId,
        ...d
      };
    });
    this.cooperationService.addMultipleData({recordList}).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.queryList();
      }
    }).finally(() => {
      this.resetFormData();
    });
  }

  // 重置数据
  resetFormData() {
    this.formData = {
      id: '',
      date: '',
      productId: '',
      registerCount: 0,
      rechargeAmount: 0,
    };
    this.modal = {
      modalVisible: false,
      operationType: '',
      loading: false,
    };
    this.modalMultiple = {
      modalVisible: false,
      operationType: '',
      loading: false,
      productId: '',
      data: []
    };
  }

  // 删除
  deleteRow(id: any) {
    this.cooperationService.deleteData(id).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.queryList();
      }
    });
  }

  addRow() {
    this.modalMultiple.data.push({
      date: '',
      registerCount: 0,
      rechargeAmount: 0,
    });
    setTimeout(() => {
      // @ts-ignore
      document.getElementById('itemList').scrollTop = document.getElementById('itemList').scrollHeight;
    });
  }
}
